package jzqb.controller;

import com.alibaba.fastjson.JSONObject;
import haha.life.common.CutPageBean;
import haha.life.common.SysTool;
import haha.life.normal.TokenUtils;
import io.swagger.annotations.ApiOperation;
import jzqb.bo.CommonsBo;
import jzqb.common.Result;
import jzqb.po.CommonsPo;
import jzqb.service.ICommonsService;
import jzqb.vo.CommonsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * Description: 个人中心--意见回复
 *
 * @author BaiGe
 * @date: 2021/4/23 10:26
 * @Version 1.0
 */
@RestController
@RequestMapping("/commons")
public class CommonsController {
    @Autowired
    private ICommonsService commonsService;


    @ApiOperation(value = "查看意见回复-前台")
    @PostMapping("/queryOwnCommons")
    public Result queryOwnCommons(@RequestBody CommonsBo commonsBo, HttpServletRequest request) {
        String userId = TokenUtils.getCurrentUserId(request);
        commonsBo.setSender(userId);
        commonsBo.setStart(commonsBo.getPage(), commonsBo.getPageSize());
        CutPageBean<CommonsVo> cutPageBean = commonsService.queryOwnCommons(commonsBo);
        return Result.succ(cutPageBean);
    }

    @ApiOperation(value = "删除意见回复-前台")
    @PostMapping("/delCommons")
    public Result delCommons(@RequestBody String params) {
        //OfFront
        JSONObject jsonObject = JSONObject.parseObject(params);
        Integer commonId = jsonObject.getInteger("commonId");
        Boolean bl = commonsService.delCommonsById(commonId);
        if (bl) {
            return Result.succ("删除成功");
        } else {
            return Result.succ("删除失败");
        }
    }

    @ApiOperation(value = "新建意见反馈-前台")
    @PostMapping("/addCommons")
    public Result addCommons(@RequestBody CommonsPo commonsPo, HttpServletRequest request) {
        String time = SysTool.getCurrentTime(SysTool.dateFormat_milli);
        commonsPo.setSendTime(time);
        commonsPo.setSender(TokenUtils.getCurrentUserId(request));
        commonsPo.setStatus("0");
        commonsPo.setReadState(1);
        Boolean bl = commonsService.addCommons(commonsPo);
        if (bl) {
            return Result.succ("添加成功");
        } else {
            return Result.succ("添加失败");
        }
    }

    @ApiOperation(value = "更改阅读状态")
    @PostMapping("/updateCommonsReadStatus")
    public Result updateCommonsReadStatus(@RequestBody CommonsPo commonsPo) {
        Boolean bl = true;
        if (commonsPo.getReadState()==0) {
            commonsPo.setReadState(1);
            bl = commonsService.updateCommonsReadStatus(commonsPo);
        }
        return Result.succ(bl);
    }


    @ApiOperation(value = "红点提示")
    @GetMapping("/getUnReadConmonsNum")
    public Result getUnReadConmonsNum(HttpServletRequest request) {
        CommonsBo commonsBo = new CommonsBo();
        commonsBo.setSender(TokenUtils.getCurrentUserId(request));
        commonsBo.setReadState(0);
        int num = (int) commonsService.getUnReadCommonsNum(commonsBo);
        return Result.succ(num);
    }

}
